package com.psc.model.a;

import com.google.gson.Gson;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.converter.PropertyConverter;
import org.greenrobot.greendao.annotation.Generated;

import java.util.Arrays;
import java.util.List;

/**
 * Creator:Yx
 * Time:2020/7/25 13:10
 * Description:首页
 * 将数据保存成String字符串，需要时由GSON解析成实体
 */
@Entity
public class HomeModeBean {
    @Id(autoincrement = true)//设置自增长
    private Long id;
    //用到了这个Convert注解，表明它们的转换类，这样就可以转换成String保存到数据库中了
    @Convert(columnType = String.class, converter = AllWatching_Converter.class)
    private AllWatchingModeBean eWatching;//大家都在看
    @Convert(columnType = String.class, converter = RVideo_Converter.class)
    private RVideoModeBean rVideo;//精选视频
    @Convert(columnType = String.class, converter = ToDay_Converter.class)
    private ToDayModeBean todaySelect;//今日精选
    //注解集合
    @Convert(columnType = String.class, converter = StringConverter.class)
    private List<String> hahaha;
    //用到了这个Convert注解，表明它们的转换类，这样就可以转换成String保存到数据库中了
   @Convert(columnType = String.class, converter = Level_Converter.class)
    private List<JavaBean> levelReport; //实体类中list数据

    //点击小锤子生成这俩构造前，需删除旧的构造，或使用@Keep annotation而不是@Generated来标记它，以保持它不受影响
    @Generated(hash = 1192881922)
    public HomeModeBean(Long id, AllWatchingModeBean eWatching, RVideoModeBean rVideo, ToDayModeBean todaySelect,
                        List<String> hahaha) {
        this.id = id;
        this.eWatching = eWatching;
        this.rVideo = rVideo;
        this.todaySelect = todaySelect;
        this.hahaha = hahaha;
    }

    @Generated(hash = 68189161)
    public HomeModeBean() {
    }


    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public AllWatchingModeBean getEWatching() {
        return this.eWatching;
    }

    public void setEWatching(AllWatchingModeBean eWatching) {
        this.eWatching = eWatching;
    }

    public RVideoModeBean getRVideo() {
        return this.rVideo;
    }

    public void setRVideo(RVideoModeBean rVideo) {
        this.rVideo = rVideo;
    }

    public ToDayModeBean getTodaySelect() {
        return this.todaySelect;
    }

    public void setTodaySelect(ToDayModeBean todaySelect) {
        this.todaySelect = todaySelect;
    }

    public List<String> getHahaha() {
        return hahaha;
    }

    public void setHahaha(List<String> hahaha) {
        this.hahaha = hahaha;
    }

    public static class AllWatching_Converter implements PropertyConverter<AllWatchingModeBean, String> {
        @Override
        public AllWatchingModeBean convertToEntityProperty(String databaseValue) {
            return new Gson().fromJson(databaseValue, AllWatchingModeBean.class);
        }

        @Override
        public String convertToDatabaseValue(AllWatchingModeBean entityProperty) {
            return new Gson().toJson(entityProperty);
        }
    }

    public static class RVideo_Converter implements PropertyConverter<RVideoModeBean, String> {
        @Override
        public RVideoModeBean convertToEntityProperty(String databaseValue) {
            return new Gson().fromJson(databaseValue, RVideoModeBean.class);
        }

        @Override
        public String convertToDatabaseValue(RVideoModeBean entityProperty) {
            return new Gson().toJson(entityProperty);
        }
    }

    public static class ToDay_Converter implements PropertyConverter<ToDayModeBean, String> {
        @Override
        public ToDayModeBean convertToEntityProperty(String databaseValue) {
            return new Gson().fromJson(databaseValue, ToDayModeBean.class);
        }

        @Override
        public String convertToDatabaseValue(ToDayModeBean entityProperty) {
            return new Gson().toJson(entityProperty);
        }
    }

    public static class StringConverter implements PropertyConverter<List<String>, String> {

        @Override
        public List<String> convertToEntityProperty(String databaseValue) {
            List<String> list = Arrays.asList(databaseValue.split(","));
            return list;
        }

        @Override
        public String convertToDatabaseValue(List<String> entityProperty) {
            StringBuilder sb = new StringBuilder();
            for (String link : entityProperty) {
                sb.append(link);
                sb.append(",");
            }
            return sb.toString();
        }
    }

    public class Level_Converter implements PropertyConverter<List<JavaBean>, String> {
        @Override
        public List<JavaBean> convertToEntityProperty(String databaseValue) {//将本地JSON字符串转成实体Bean
            if (databaseValue == null) {
                return null;
            }
             // 先得获得这个，然后再typeToken.getType()，否则会异常
            TypeToken<List<JavaBean>> typeToken = new TypeToken<List<JavaBean>>(){};
            return new Gson().fromJson(databaseValue, typeToken.getType());
        }

       @Override
       public String convertToDatabaseValue(List<JavaBean> arrays) {//将返回数据转成JSON字符串保存到本地
               if (arrays == null||arrays.size()==0) {
                   return "";//此处不能返回null不然保存数据的时候会报the bind value at index 2 is null
               } else {
                   String sb = new Gson().toJson(arrays);
                   return sb;
               }
       }
   }

}
